
package com.scjyzr.mysweeper;

import com.adwo.adsdk.AdwoAdView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class GameMainActivity extends Activity {

    private int currentSelect = 0;// 当前难关选择。

    /**
     * 获取当前难度等级。
     * 
     * @return
     */
    public int getCurrentSelect() {
        return currentSelect;
    }

    /**
     * 设置当前难度选择。
     * 
     * @param currentSelect
     */
    public void setCurrentSelect(int currentSelect) {
        this.currentSelect = currentSelect;
    }

    /**
     * 重新从xml文件中获取当前难度等级。
     */
    public void resetCurrentDiffLevel() {
        // 这下面的顺序和R.array.select_dialog_diff_level一致
        currentSelect = MyUtil.getDiffLevel(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_game_main);
        AdwoAdView.setBannerMatchScreenWidth(true);
        Button beginButton = (Button) findViewById(R.id.begin_button);
        Button diffButton = (Button) findViewById(R.id.diffict_button);

        Button hisButton = (Button) findViewById(R.id.his_button);

        beginButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startOtherActivity();
            }
        });

        diffButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog();
            }
        });

        StatisiticUtil.functiontSatistics(this, StatisiticUtil.StatisticKey.OPEN_GAME);

        hisButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showHisDialog();
            }
        });

    }

    private void showHisDialog() {


        StatisiticUtil.functiontSatistics(this, StatisiticUtil.StatisticKey.HIS_LEVEL);
        int score = MyUtil.getScore(this);
        String message = this.getString(R.string.no_his_score);
        if (score > 0) {
            message = String.format(this.getString(R.string.his_score), score);
        }
        AlertDialog dia = MyUtil.showOkCancle(this, null, message, 0);
        dia.show();
    }

    private void showDialog() {
        StatisiticUtil.functiontSatistics(this, StatisiticUtil.StatisticKey.SELECT_DIFF);
        resetCurrentDiffLevel();
        AlertDialog dialog = MyUtil.showDiffDialogForSelect(this);
        dialog.show();
    }

    private void startOtherActivity() {
        StatisiticUtil.functiontSatistics(this, StatisiticUtil.StatisticKey.START_GAME);
        Intent intent = new Intent(this, GameActivity.class);
        this.startActivity(intent);

    }

    @Override
    public void onResume() {
        super.onResume();
        StatisiticUtil.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        StatisiticUtil.onPause(this);
    }

}
